
//sdp_ui 负责对外提供业务层面的api


var config = require('../../conf/config.js');
var server_host = config.sdp_server.host;
var server_port = config.sdp_server.port;

var sdp_server = server_host + ':' +  server_port;


/**
 * 获取当前用户信息
 * @param
 * @returns {*}
 */
exports.getCurrentUser = function (cb) {
    return $.ajax({
        type: "GET",
        url: "/api/user"
    }, cb);
};

/**
 * 获取当前用户已订阅的商品信息
 * @param user_shortId
 * @param cb
 * @returns {*}
 */
exports.getOrders = function (user_shortId, cb) {
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/order_list/" + user_shortId
    }, cb);
};
//
//exports.updateProject = function (project_id,options, cb) {
//    return $.ajax({
//        type: "PATCH",
//        data: options,
//        url: "/api/project/" + project_id
//    }, cb);
//};
exports.createProject = function (options, cb) {
    return $.ajax({
        type: "POST",
        url: "/api/project",
        data: options
    }, cb);
};
/**
 * 获取当前用户创建的|参与的|所有相关 项目
 * @param role 角色 owner|member|all
 * @param cb
 * @returns {*}
 */
exports.getProjects = function (user, role, cb) {
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/projects?user=" + user + "&role=" + role
    }, cb);
};

exports.getCurrentProject = function (user,cb) {
    return $.ajax({
        type: "GET",
        url: "/api/project?user=" + user
    }, cb);
};
//获取所有项目信息
exports.getAllProjects = function (cb) {
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/all_projects"
    }, cb);
};

/**
 * 根据project_id获取项目信息
 * @param project_id
 * @param cb
 * @returns {*}
 */
exports.getProject = function (project_id, cb) {
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/project/" + project_id
    }, cb);
};

/**
 * 为项目添加成员
 * @param options
 *{
    projectId:56e960a905ac648c0b8bbb46
    projectTitle:项目2
    user:zwz
    role:admin
  }
 * @param cb
 * @returns {*}
 */
exports.addMember = function (options, cb) {
    return $.ajax({
        type: "POST",
        url: "/api/member",
        data: options
    }, cb);
};

/**
 * 删除member中 指定_id 的成员信息
 * @param options  //Array 存放待删除的member objectId
 *  {
       member_id:['_id1','_id2']
    }
 * @param cb
 * @returns {*}
 */
exports.removeMember = function (options, cb) {
    return $.ajax({
        type: "DELETE",
        url: "/api/member",
        traditional: true,
        data: options
    }, cb);
};

/**
 * 修改成员角色
 * @param options
 * {
    _id:56e9611405ac648c0b8bbb49     //member中 objectId
    role:developer                  //修改后成员角色
   }
 * @param cb
 * @returns {*}
 */
exports.updateMember = function (options, cb) {
    return $.ajax({
        type: "PUT",
        url: "/api/member",
        data: options
    }, cb);
};

/**
 * 获取_id=projectId 的项目中的所有成员
 * @param projectId
 * @param cb
 * @returns {*}
 */
exports.getMembers = function (projectId, cb) {
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/member/" + projectId
    }, cb);
};

/**
 * 添加证书
 * @param options
 * {
    "type":"iosCert",  //证书类型
    "name":"ioscert1", //证书名称
    "files":[          //证书文件
              {
                "file_name":"cert",
                "file_path":"path"
              },
              {
                "file_name":"cert2",
                "file_path":"path2"
              }
            ],
    "extend_info":          //证书扩展信息
               {
                 "p12certSecret":"secret",
                 "authID":"110201203013"
                }
}
 *
 * @param cb
 * @returns {*}
 */
exports.addCert = function ( project_id , options, cb) {
    if(options){
        options = JSON.stringify(options);
    }
    var args = {
        type: "POST",
        url: "/api/cert/"+ project_id,
        data:options
    };
    args.contentType = 'application/json';
    args.processData = false;
    return $.ajax(args, cb);
};


exports.getMembersByShortIdArr = function(options,cb){
    if(options){
        options = JSON.stringify(options);
    }

    var args = {
        type: "POST",
        url: "/api/members",
        data:options
    };
    args.contentType = 'application/json';
    args.processData = false;
    return $.ajax(args, cb);
};

/**
 * 删除证书
 * @param options
 * @param cb
 * @returns {*}
 */
exports.removeCert = function (options, cb) {
    return $.ajax({
        type: "DELETE",
        url: "/api/project/removeCert",
        data: options
    }, cb);
};


/**
 * 获取证书信息
 * @param cert_id
 * @param cb
 * @returns {*}
 */
exports.getCert = function (cert_id, cb) {
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/cert/" + cert_id
    }, cb);
};

exports.getCerts = function (project_id,cb) {
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/certs/"+project_id
    }, cb);
};


/**
 * 修改证书信息
 * @param options
 * {
    "cert_id":"56ea18c4e80bf8d01db936ee",
    "name":"ioscert11111",
    "files":[
              {
                "file_name":"cert123123123",
                "file_path":"path111121313"
              },
              {
                "file_name":"cert2213123123",
                "file_path":"path21231231231"
              }
            ],
    "extend_info":
               {
                 "p12certSecret":"secret12312312",
                 "authID":"11020120301312312312313"
                }
}
 * @param cb
 * @returns {*}
 */
exports.modifyCert = function (id,options, cb) {
    return $.ajax({
        type: "PUT",
        url: "/api/cert/" + id,
        data: options,
        cache: false,
        processData: false,
        contentType: false,
    }, cb);
};

exports.addDependency = function (project_id, options, cb) {
    return $.ajax({
        type: "POST",
        url: "/api/dependency/" + project_id,
        data: options
    }, cb);
};

exports.removeDependency = function (project_id, options, cb) {
    return $.ajax({
        type: "DELETE",
        url: "/api/dependency/" + project_id,
        data: options
    }, cb);
};

exports.updateDependency = function (project_id, options, cb) {
    return $.ajax({
        type: "PUT",
        url: "/api/dependency/" + project_id,
        data: options,
        traditional: true
    }, cb);
};

exports.updateDependencies = function (project_id, options, cb) {
    if(options){
        options = JSON.stringify(options);
    }
    var args = {
        type: "PUT",
        url: "/api/dependencies/" + project_id,
        data:options
    };
    args.contentType = 'application/json';
    args.processData = false;
    return $.ajax(args, cb);
    //return $.ajax({
    //    type: "PUT",
    //    url: "/api/dependencies/" + project_id,
    //    data: options
    //}, cb);
};

//exports.addBuildConfig = function (options ) {
//    return $.ajax({
//        type: "POST",
//        url: "/api/buildConfig",
//        data: options
//    } );
//};
//
//
//exports.getBuildConfig = function (cfg_id ) {
//    return $.ajax({
//        type: "GET",
//        url: "/api/buildConfig/" + cfg_id
//    } );
//};
//
//exports.editBuildConfig = function (options ) {
//    return $.ajax({
//        type: "PUT",
//        url: "/api/buildConfig",
//        data: options
//    } );
//};

//exports.upload = function (path, space, fileName ) {
//    return $.ajax({
//        type: "GET",
//        url: "/api/upload?space=" + space + '&path=' + path + '&fileName=' + fileName,
//    } );
//};

exports.addWorkspace = function (options, cb) {
    return $.ajax({
        type: "POST",
        url: "/api/workspace/add",
        data: options
    }, cb);
};

exports.addWkpMembers = function (workspace_id,options, cb) {
    return $.ajax({
        type: "POST",
        url: "/api/workspace/member/"+workspace_id,
        data: options
    }, cb);
};
exports.removeWkpMembers = function (workspace_id,shortId, cb) {
    return $.ajax({
        type: "DELETE",
        url: "/api/workspace/member/"+workspace_id+"?user="+shortId,
    }, cb);
};
exports.editWkpMembers = function(workspace_id,options, cb){
    return $.ajax({
        type: "PUT",
        url: "/api/workspace/member/"+workspace_id,
        data: options
    }, cb);
};

exports.getWorkspaceById = function (workspace_id, cb) {
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/workspace/" + workspace_id
    }, cb);
};
exports.getWorkspaces = function (project_id, cb) {
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/workspace/list/" + project_id,
    }, cb);
};

//获取所有项目信息
exports.getAllWorkspaces = function (cb) {
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/workspaces"
    }, cb);
};



exports.deleteWorkspace = function (workspace_info, cb) {
    return $.ajax({
        type: "POST",
        url: "/api/workspace/delete",
        data:workspace_info
    },cb);
};

//检查git url是否有效
//exports.checkGitUrl = function (options, cb) {
//    return $.ajax({
//        type: "POST",
//        url: "/api/workspace/git/check",
//        data:options
//    }, cb);
//};


exports.getProjectsInMasterRepo = function(project_id,cb){
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/workspace/projects/"+project_id,
    }, cb);
};

exports.getDependencies = function (work_id, cb) {
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/workspace/dependencies/" + work_id
    }, cb);
};

exports.getLocalRepo = function (cb) {
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/local_repo"
    }, cb);
};

exports.updateWkpDependency = function (options, cb) {
    return $.ajax({
        type: "POST",
        url: "/api/workspace/dependencies/update",
        data: options
    }, cb);
};

//exports.updateTargetByProjectId = function (options, cb) {
//    return $.ajax({
//        type: "POST",
//        url: "/api/workspace/dependencies/updateTarget",
//        data: options
//    }, cb);
//};

exports.deleteDependency = function (options, cb) {
    return $.ajax({
        type: "POST",
        url: "/api/workspace/dependencies/delete",
        data: options
    }, cb);
};

//{fileName:"",version:"",other_info:""}
exports.downloadToRepo = function (options,cb) {
    return $.ajax({
        type: "POST",
        url: "/api/local_repo/download",
        data:options
    }, cb);
};

// * @param：{fileName:"",version:""}
exports.updateRepo = function (options,cb) {
    return $.ajax({
        type: "PUT",
        url: "/api/local_repo/update",
        data:options
    }, cb);
};

exports.getAllProductConfigs = function(project_id,cb){
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/product_configs?project="+project_id
    }, cb);
};

exports.getProductConfigById = function(cfg_id,cb){
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/product_config/" + cfg_id
    }, cb);
};

exports.updateProductConfig = function(cfg_id,options,cb){
    if(options){
        options = JSON.stringify(options);
    }
    var args = {
        type: "PUT",
        url: "/api/product_configs/" + cfg_id,
        //traditional: true,
        data:options
    };
    args.contentType = 'application/json';
    args.processData = false;
    return $.ajax(args, cb);
};

exports.addProductConfig = function(options,cb){
    if(options){
        options = JSON.stringify(options);
    }

    var args = {
        type: "POST",
        url: "/api/product_configs",
        //traditional: true,
        data:options
    };
    args.contentType = 'application/json';
    args.processData = false;
    return $.ajax(args, cb);
};

exports.deleteProductConfig = function(config_id,cb){
    return $.ajax({
        type: "DELETE",
        url: "/api/product_configs/" + config_id,
    }, cb);
};


exports.getAllBuildConfigs = function(cb){
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/buildConfigs"
    }, cb);
};

exports.getBuildConfigInfo = function(config_id,cb){
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/buildConfig/"+config_id
    }, cb);
};

exports.addBuildConfig = function(options,cb){
    return $.ajax({
        type: "POST",
        url: "/api/buildConfig",
        traditional: true,
        data:options
    }, cb);
};

exports.updateBuildConfig = function(options,cb){
    if(options){
        options = JSON.stringify(options);
    }

    var args = {
        type: "PUT",
        url: "/api/buildConfig",
        traditional: true,
        data:options
    };
    args.contentType = 'application/json';
    args.processData = false;
    return $.ajax(args, cb);
};

exports.deleteBuildConfig = function(config_id,cb){
    return $.ajax({
        type: "DELETE",
        url: "/api/buildConfig/" + config_id,
    }, cb);
};

exports.executeBuilding = function(options,cb){
    return $.ajax({
        type: "POST",
        url: "/api/builds",
        data:options
    }, cb);
};

exports.getBuildResultById = function(id,cb){
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/builds/" + id
    }, cb);
};
//exports.cancelBuilding = function(data ){
//    return $.ajax({
//        type: "POST",
//        url: "/api/build/cancel",
//        data:data
//    } );
//};
exports.getAllBuilds = function(cb ){
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/builds"
    }, cb);
};
exports.deleteBuildResult = function(build_id,cb){
    return $.ajax({
        type: "DELETE",
        url: "/api/builds/" + build_id,
    }, cb);
};
exports.getAllPublishConfigs = function(cb){
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/publishConfigs"
    }, cb);
};
exports.getPublishConfigById = function(config_id,cb){
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/publishConfig/"+config_id
    }, cb);
};
exports.addPublishConfig = function(options,cb){
    if(options){
        options = JSON.stringify(options);
    }
    var args = {
        type: "POST",
        url: "/api/publishConfig",
        data:options
    };
    args.contentType = 'application/json';
    args.processData = false;
    return $.ajax(args, cb);
};

exports.deletePublishConfig = function(config_id,cb){
    return $.ajax({
        type: "DELETE",
        url: "/api/publishConfig/" + config_id,
    }, cb);
};

exports.updatePublishConfig = function(publishConfig_id,options,cb){
    if(options){
        options = JSON.stringify(options);
    }

    var args = {
        type: "PUT",
        url: "/api/publishConfig/" + publishConfig_id,
        traditional: true,
        data:options
    };
    args.contentType = 'application/json';
    args.processData = false;
    return $.ajax(args, cb);
};

exports.getPublishRec = function(publish_config_id,cb){
    return $.ajax({
        type: "GET",
        url: sdp_server + "api/getPublishRec/"+ publish_config_id
    }, cb);
};

exports.deletePublish = function(publish_id,cb){
    return $.ajax({
        type: "DELETE",
        url: "/api/publishRec/" + publish_id,
    }, cb);
};

exports.getGoodsInfo = function(config_id,cb){
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/publishGoodsInfo/"+ config_id
    }, cb);
};
exports.publish = function(options,cb){
    if(options){
        options = JSON.stringify(options);
    }
    var args = {
        type: "POST",
        url: "/api/publish",
        data:options
    };
    args.contentType = 'application/json';
    args.processData = false;
    return $.ajax(args, cb);
};

//数据源
//exports.getDataSources = function(cb){
//    return $.ajax({
//        type: "GET",
//        url: sdp_server + "/api/data_sources"
//    }, cb);
//};
//
//exports.getDataSourceById = function(cfg_id,cb){
//    return $.ajax({
//        type: "GET",
//        url: sdp_server + "/api/data_source/" + cfg_id
//    }, cb);
//};
//
//exports.updateDataSource = function(cfg_id,options,cb){
//    if(options){
//        options = JSON.stringify(options);
//    }
//    var args = {
//        type: "PUT",
//        url: "/api/data_sources/" + cfg_id,
//        //traditional: true,
//        data:options
//    };
//    args.contentType = 'application/json';
//    args.processData = false;
//    return $.ajax(args, cb);
//};
//
//exports.addDataSource = function(options,cb){
//    if(options){
//        options = JSON.stringify(options);
//    }
//
//    var args = {
//        type: "POST",
//        url: "/api/data_sources",
//        //traditional: true,
//        data:options
//    };
//    args.contentType = 'application/json';
//    args.processData = false;
//    return $.ajax(args, cb);
//};
//
//exports.deleteDataSource = function(config_id,cb){
//    return $.ajax({
//        type: "DELETE",
//        url: "/api/data_sources/" + config_id,
//    }, cb);
//};
exports.searchUsers = function (keywords,cb) {
    return $.ajax({
        type: "GET",
        url: sdp_server + "/api/user/search?keywords=" + keywords
    }, cb);
};
